<template>
    <view class="news-list">
        <!--单个新闻-->
        <view class="news-item" @tap="tourl" :data-index="index" v-for="(item, index) in newsData" :key="index">
            {{ item.source }}

            <view class="news-item-info">
                <view class="header">
                    <text class="channelName" :data-index="index">{{ item.channelName }}</text>
                    <text class="source" :data-index="index">{{ item.source }}</text>
                </view>
                <view class="title" :data-index="index">{{ item.title }}</view>
                <view class="time" :data-index="index">{{ item.pubDate }}</view>
            </view>

            <view class="news-item-image" v-if="item.imageurls[0] != null">
                <image
                    :src="item.imageurls[0].url"
                    v-if="item.imageurls[0] != null"
                    style="width: 100%; height: 100%"
                    data-url="/pages/details/details"
                    :data-index="index"
                    mode="aspectFill"
                ></image>
            </view>
        </view>
    </view>
</template>

<script>
// pages/newsItem/newsItem.js
var app = getApp();
export default {
    data() {
        return {
            newsData: [],
            page: 1
        };
    },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        this.search();
    },
    onReady: function () {
        // 页面渲染完成
    },
    onShow: function () {
        // 页面显示
    },
    onHide: function () {
        // 页面隐藏
    },
    onUnload: function () {
        // 页面关闭
    },
    //上拉加载
    onReachBottom: function () {
        uni.showToast({
            title: '正在加载',
            icon: 'loading',
            duration: 1000
        });
        var that = this;
        that.page = that.page + 1;
        uni.request({
            url: 'https://route.showapi.com/109-35',
            data: {
                channelId: '',
                channelName: '',
                maxResult: '15',
                needAllList: '',
                needContent: '1',
                needHtml: '',
                page: that.page,
                showapi_appid: '860473',
                title: getApp().globalData.title,
                showapi_sign: '772248c40b324c43b245b027004b528a',
                showapi_timestamp: ''
            },
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                var list = res.data.showapi_res_body.pagebean.contentlist;
                console.log(getApp().globalData.title, list);

                if (list.length != 0) {
                    list.forEach(function (obj) {
                        that.newsData.push(obj);
                    });
                    that.setData({
                        newsData: that.newsData,
                        page: that.page
                    });
                } else {
                    uni.showToast({
                        title: '没有了',
                        icon: 'loading',
                        duration: 1000
                    });
                }
            },
            fail: function () {
                request.error();
            }
        });
    },
    methods: {
        //搜索页页
        search: function () {
            var title = getApp().globalData.title;
            var that = this;
            uni.request({
                url: 'https://route.showapi.com/109-35',
                data: {
                    channelId: '',
                    channelName: '',
                    maxResult: '15',
                    needAllList: '',
                    needContent: '1',
                    needHtml: '',
                    page: '1',
                    showapi_appid: '860473',
                    title: title,
                    showapi_sign: '772248c40b324c43b245b027004b528a',
                    showapi_timestamp: ''
                },
                header: {
                    'content-type': 'application/json'
                },
                success: function (res) {
                    that.newsData = res.data.showapi_res_body.pagebean.contentlist;
                    console.log('数据', res.data.showapi_res_body.pagebean);
                    that.setData({
                        newsData: that.newsData
                    });
                },
                fail: function () {
                    request.error();
                }
            });
        },

        tourl: function (e) {
            // object转换get字符串
            var index = e.target.dataset.index;
            var temp = this.newsData[index];
            app.globalData.array = temp;
            uni.navigateTo({
                url: '/pages/details/details'
            });
        }
    }
};
</script>
<style>
@import './newsItem.css';
</style>
